import 'dart:math';

import 'package:flutter/material.dart';
import 'package:qimeng/Colorgamedata.dart';
import 'package:qimeng/colorall.dart';

import 'Zimugamedata.dart';

class ZimuGame extends StatefulWidget {
  const ZimuGame({super.key});

  @override
  State<ZimuGame> createState() => _ZimuGameState();
}

class _ZimuGameState extends State<ZimuGame> {
  PageController _controller = PageController();

  int count = 0;
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    List<ZimuGameData> list = [
      ZimuGameData(
        color: Color(0xffE86E6D),
        texttip: 'a',
        tip: 'apple',
        text: 'A',
        img: 'assets/切图/测试-颜色-选项1.png',
      ),
      ZimuGameData(
        color: Color(0xffF8C160),
        texttip: 'b',
        tip: 'banana',
        text: 'B',
        img: 'assets/切图/测试-颜色-选项1.png',
      ),
      ZimuGameData(
        color: Color(0xffE78426),
        texttip: 'c',
        tip: 'cat',
        text: 'C',
        img: 'assets/切图/测试-颜色-选项1.png',
      ),
      ZimuGameData(
        color: Color(0xffF3C071),
        texttip: 'd',
        tip: 'dog',
        text: 'D',
        img: 'assets/切图/测试-颜色-选项1.png',
      ),
      ZimuGameData(
        color: Color(0xff426734),
        texttip: 'e',
        tip: 'elephant',
        text: 'E',
        img: 'assets/切图/测试-颜色-选项1.png',
      ),
      ZimuGameData(
        color: Color(0xff3C98A7),
        texttip: 'f',
        tip: 'fish',
        text: 'F',
        img: 'assets/切图/测试-颜色-选项1.png',
      ),
    ];

    Set<int> randomNumbers = Set<int>();

    while (randomNumbers.length < 4) {
      int randomNumber = Random().nextInt(list.length - 1) + 1;
      randomNumbers.add(randomNumber);
    }
    int trueZimuTest = Random().nextInt(3) + 1;
    List<int> zimuTest = randomNumbers.toList();
    return Scaffold(
      backgroundColor: Kbgcolor,
      appBar: AppBar(
        elevation: 0,
        leadingWidth: 100,
        scrolledUnderElevation: 0,
        backgroundColor: Kbgcolor,
        toolbarHeight: size.height * 0.1,
        leading: InkWell(
          onTap: () {
            Navigator.pop(context);
          },
          child: Image.asset(
            'assets/切图/Home组件.png',
            scale: 0.6,
            fit: BoxFit.cover,
          ),
        ),
        actions: [Image.asset('assets/切图/组 17.png')],
      ),
      body: Stack(
        children: [
          PageView(
            onPageChanged: (value) {
              print(value);
              setState(() {
                count = value;
              });
            },
            // physics: NeverScrollableScrollPhysics(),
            controller: _controller,
            children: List.generate(list.length + 2, (index) {
              if (index == list.length + 1) {
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: size.width * 0.03),
                  child: Column(
                    children: [
                      Text(
                        '以下哪个是${list[zimuTest[trueZimuTest]].text}的？',
                        style: TextStyle(
                            color: Color(0xffFD7968),
                            fontSize: size.width * 0.1,
                            fontWeight: FontWeight.w900,
                            shadows: [
                              Shadow(
                                  color: Colors.black.withOpacity(0.16),
                                  blurRadius: 6,
                                  offset: Offset(0, 3))
                            ]),
                      ),
                      ListView(
                          shrinkWrap: true,
                          children: List.generate(4, (index) {
                            return InkWell(
                              onTap: () {
                                if (index == trueZimuTest) {
                                  Navigator.pop(context);
                                }
                              },
                              child: Container(
                                height: size.width * 0.25,
                                decoration: BoxDecoration(
                                    image: DecorationImage(
                                        image: AssetImage(
                                            list[zimuTest[index]].img)),
                                    borderRadius: BorderRadius.circular(20),
                                    boxShadow: [
                                      BoxShadow(
                                          color: Color(0xffA0A0A0)
                                              .withOpacity(0.16),
                                          blurRadius: 24,
                                          offset: Offset(0, 24)),
                                    ]),
                              ),
                            );
                          }))
                    ],
                  ),
                );
              } else if (index == list.length) {
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: size.width * 0.03),
                  child: Column(
                    children: [
                      Text(
                        '哪个方块是${list[zimuTest[trueZimuTest]].text}？',
                        style: TextStyle(
                            color: Color(0xffFD7968),
                            fontSize: size.width * 0.1,
                            fontWeight: FontWeight.w900,
                            shadows: [
                              Shadow(
                                  color: Colors.black.withOpacity(0.16),
                                  blurRadius: 6,
                                  offset: Offset(0, 3))
                            ]),
                      ),
                      GridView.count(
                          shrinkWrap: true,
                          crossAxisCount: 2,
                          crossAxisSpacing: 20,
                          mainAxisSpacing: 20,
                          children: List.generate(
                            4,
                            (index) => InkWell(
                              onTap: () {
                                if (index == trueZimuTest) {
                                  _controller.jumpToPage(count + 1);
                                }
                              },
                              child: Container(
                                height: size.width * 0.25,
                                decoration: BoxDecoration(
                                    color: list[zimuTest[index]].color,
                                    // color: colorTest[index],
                                    borderRadius: BorderRadius.circular(20),
                                    boxShadow: [
                                      BoxShadow(
                                          color: Color(0xffA0A0A0)
                                              .withOpacity(0.16),
                                          blurRadius: 24,
                                          offset: Offset(0, 24)),
                                    ]),
                              ),
                            ),
                          ))
                    ],
                  ),
                );
              } else {
                return ZimuGameItem(
                  size: size,
                  data: list[index],
                );
              }
            }),
          ),
          Visibility(
            visible: count != list.length + 1 && count != list.length,
            child: Positioned(
                bottom: 100,
                right: 0,
                child: Container(
                  width: size.width * 0.8,
                  height: size.width * 0.15,
                  decoration: BoxDecoration(
                    boxShadow: [
                      BoxShadow(
                          color: Colors.black.withOpacity(0.16),
                          offset: Offset(0, 3),
                          blurRadius: 6)
                    ],
                    color: Color(0xffFFE3E2),
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(30),
                        bottomLeft: Radius.circular(30)),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      InkWell(
                        onTap: () {
                          _controller.jumpToPage(list.length);
                        },
                        child: Image.asset('assets/切图/biz-test.png'),
                      ),
                      InkWell(
                        onTap: () {
                          if (count == 0) {
                          } else {
                            _controller.jumpToPage(count - 1);
                          }
                        },
                        child: Image.asset('assets/切图/Left-5.png'),
                      ),
                      InkWell(
                        onTap: () {
                          if (count == list.length - 1) {
                            _controller.jumpToPage(count + 1);
                          } else {
                            _controller.jumpToPage(count + 1);
                          }
                        },
                        child: Image.asset('assets/切图/Left-1.png'),
                      ),
                    ],
                  ),
                )),
          )
        ],
      ),
    );
  }
}
// padding: EdgeInsets.symmetric(horizontal: size.width * 0.03),

class ZimuGameItem extends StatelessWidget {
  const ZimuGameItem({
    super.key,
    required this.size,
    required this.data,
  });
  final ZimuGameData data;
  final Size size;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: size.width * 0.03),
      child: Column(
        children: [
          SizedBox(
            height: size.height * 0.05,
          ),
          Row(
            children: [
              Expanded(
                  child: RichText(
                text: TextSpan(
                  style: TextStyle(fontSize: 160, color: data.color),
                  text: data.text,
                  children: [
                    TextSpan(
                      text: data.texttip,
                      style: TextStyle(fontSize: 80),
                    ),
                  ],
                ),
              )),
              Column(
                children: [
                  Image.asset(
                    data.img,
                    scale: 2.5,
                  ),
                  Image.asset(
                    data.img,
                    scale: 2.5,
                  ),
                  Text(
                    data.tip,
                    style: TextStyle(
                        fontSize: 50,
                        color: Color(0xffF5F5F5),
                        shadows: [
                          Shadow(
                              color: Colors.black.withOpacity(0.16),
                              blurRadius: 6,
                              offset: Offset(0, 3))
                        ]),
                  ),
                ],
              ),
            ],
          ),
          SizedBox(
            height: size.height * 0.05,
          ),
        ],
      ),
    );
  }
}
//浮在上面点击左右切换???麻烦吗但是好像只能这样做哈哈
